package com.zr.bookcorner.presenter;

import android.content.Context;

import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.model.BookModel;
import com.zh.baselibrary.netutils.BaseResp;
import com.zh.baselibrary.rxjava.RxSubscribe;
import com.zh.baselibrary.utils.PhoneUtil;
import com.zh.baselibrary.utils.ShareManager;
import com.zh.baselibrary.views.ToastView;
import com.zh.model.cabinet.UserInfo;
import com.zh.model.manager.SearchBook;
import com.zh.model.request.LoginInfo;
import com.zr.andealib.util.LockUtil;
import com.zr.bookcorner.constant.Constant;
import com.zr.bookcorner.presenter.view.QueryView;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class QueryPresenter extends BasePresenter<QueryView> {
    private BookModel bookModel;
    private LockUtil lockUtil;
    private int boxNum;
    String schoolId;
    String uuid;


    @Override
    public void attachModel(Context context) {
        super.attachModel(context);
        bookModel = new BookModel(context);
        schoolId = ShareManager.getValue(context, BaseConstant.DEVICE_SCHOOL_ID);
        uuid = PhoneUtil.getUUID(context);
    }

    public void initLock(){
        if (lockUtil == null) {
            lockUtil = new LockUtil();
            lockUtil.init();
        }
    }
    public void onDestroy() {
        if (lockUtil != null) {
            lockUtil.destory();
            lockUtil = null;
        }
    }
    public void checkDoorStatus(int boxNum) {
        this.boxNum = boxNum;
        if (lockUtil.isDoorOPen(boxNum)) {
            ToastView.showLong("当前柜门开启，请先关闭");
        } else {
            getView().showDialog();
        }
    }
    public void checkCard(String cardCode) {
        String uuid = PhoneUtil.getUUID(getContext());
        String schoolId = ShareManager.getValue(getContext(), BaseConstant.DEVICE_SCHOOL_ID);
        Observable<BaseResp<UserInfo>> observable = getObservableNotFilter(bookModel.login(schoolId,new LoginInfo(cardCode,uuid,0)));
        observable.subscribe(new RxSubscribe<BaseResp<UserInfo>>() {
            @Override
            public void onSuccess(BaseResp<UserInfo> userInfoBaseResp) throws Exception {
                if(userInfoBaseResp.getCode()==0) {
                    UserInfo info = userInfoBaseResp.getData();
                    ShareManager.setValue(getContext(), BaseConstant.TOKEN_ID,
                            info.getToken());
                    ShareManager.setValue(getContext(), Constant.LAST_USER_ID,
                            info.getId() + "");
                    ShareManager.setValue(getContext(), Constant.LAST_USER_TYPE,
                            info.getType() + "");
                    lockUtil.openDoor(boxNum);
                    ShareManager.setInt(getContext(), Constant.LAST_OPEN, boxNum);
                    getView().onSuccess(null);
                }else{
                    getView().defyError();
                    ToastView.showLong(userInfoBaseResp.getMsg());
                }
            }

            @Override
            public void onHint(String hint) {
                ToastView.showLong(hint);
                getView().defyError();
            }
        });

    }
    public void queryBook(String content,int pageNum) {
        bookModel.queryBook(schoolId, content, uuid, pageNum, 10)
                .subscribeOn(Schedulers.io())//启动订阅线程
                .observeOn(AndroidSchedulers.mainThread())//切换至主线程
                .subscribe(new RxSubscribe<BaseResp<SearchBook>>() {
                    @Override
                    public void onSuccess(BaseResp<SearchBook> listBaseResp) throws Exception {
                        if (listBaseResp.isSuccess()) {
                             getView().getDataSuccess(listBaseResp.getData());
                        }else
                            getView().onHint(listBaseResp.getMsg());
                    }

                    @Override
                    public void onHint(String hint) {
                        getView().onHint(hint);
                    }
                });
    }
    public void count() {
        bookModel.selectCount(schoolId)
                .subscribeOn(Schedulers.io())//启动订阅线程
                .subscribe(new RxSubscribe<BaseResp<Object>>() {
                    @Override
                    public void onSuccess(BaseResp<Object> listBaseResp) throws Exception {
                    }
                    @Override
                    public void onHint(String hint) {

                    }
                });
    }

}
